﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace Controllers
{
    public class DutyController : BaseController
    {
        //分页查询
        public IQueryable<DutyDTO> Find(int start, int limit, out int cnt)
        {
            IQueryable<DateTime> q = (from r in ctx.Duties select r.DutyDate).Distinct().OrderByDescending(r=>r.Date);
            cnt = q.Count();
            IQueryable<DateTime> qLimit = q.Skip(start).Take(limit);
            IQueryable<DutyDTO> list = from r in ctx.Duties
                                       where qLimit.ToArray().Contains(r.DutyDate)
                                       join e in ctx.Employees on r.EmpId equals e.Id into t
                                        from x in t.DefaultIfEmpty()
                                        select new DutyDTO
                                         {
                                             DutyDate = r.DutyDate,
                                             DeptId = r.DeptId,
                                             Name =x.Name,
                                             TelExt =x.TelExt
                                         };
            return list;
        }

        //获取各部门值班人员
        public IQueryable<DutyDTO> GetDutier()
        {
            int[] deptIdArrs = { 17, 18, 30, 21, 22, 39,73,74,75, 52, 34, 35, 67, 16, 19, 37 };//结算部17,业务部18(呼叫中心前台业务组30),行政部(行政中心3)21+黄江基地22,物流中心6(物流部39(73,74,75),供应部52),资金管理部35(财务中心4),36,67,产品部12,直营店19,会议管理部37
            IQueryable<DutyDTO> list = from r in ctx.Employees where r.LeaveDate==null
                                       join r2 in ctx.EmpDepPositions on r.Id equals r2.EmpId
                                       where r2.PosId <= 8 && r2.PosId >= 3 || deptIdArrs.Contains(r2.DeptId)
                                       select new DutyDTO { DeptId = r2.DeptId, PosId=r2.PosId,EmpId = r.Id, Name = r.Name };
            return list;
        }

        //保存值班计划
        public ActionResult Add(string json)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            List<Duty> jsArray = (List<Duty>)JavaScriptConvert.DeserializeObject(json, typeof(List<Duty>));
            try
            {
                ctx.Duties.InsertAllOnSubmit(jsArray);
                ctx.SubmitChanges();
                rdto.Message = "新增成功";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = "新增失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }

        //按日期删除值班安排
        public string Del(DateTime date)
        {
            string retStr = "Succ";
            try
            {
                IQueryable<Duty> list = ctx.Duties.Where(r => r.DutyDate == date);
                ctx.Duties.DeleteAllOnSubmit(list);
                ctx.SubmitChanges();
            }
            catch (Exception ex)
            {
                retStr = "Fail";
            }
            return retStr;
        }
    }
}